// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   REALBigDecimalImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.QTY;
import com.sunwayhorizo.health.hl7.datatypes.REAL;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.math.BigDecimal;

import java.text.StringCharacterIterator;

import java.util.StringTokenizer;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, INTImpl, NullFlavorImpl, 
//            STImpl, TYPEImpl

public class REALBigDecimalImpl extends ANYImpl
    implements REAL
{

    public static final String RCS_ID = "$Header: REALBigDecimalImpl.java 115.18 2005/07/15 11:45:45 anbindal noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: REALBigDecimalImpl.java 115.18 2005/07/15 11:45:45 anbindal noship $", "oracle.apps.ctb.hl7.types.common");
    private BigDecimal mValue;

    protected REALBigDecimalImpl(BigDecimal bigdecimal)
    {
        if(bigdecimal == null)
        {
            throw ANYImpl.newNullException("REAL<BigDecimal>", "constructor", "value");
        } else
        {
            mValue = bigdecimal;
            return;
        }
    }

    protected REALBigDecimalImpl(CS cs)
    {
        super(true, cs);
        mValue = null;
    }

    public static REAL valueOf(BigDecimal bigdecimal)
    {
        return new REALBigDecimalImpl(bigdecimal);
    }

    public static REAL nullREAL(CS cs)
    {
        return new REALBigDecimalImpl(cs);
    }

    public static REAL nullREAL()
    {
        return new REALBigDecimalImpl(((CS) (null)));
    }

    public BL equal(ANY any)
    {
        if(any == this)
            return BLImpl.TRUE;
        if(!(any instanceof REAL))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            REAL real = (REAL)any;
            return BLImpl.valueOf(mValue.equals(real.bigDecimalValue()));
        }
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL;
    }

    public ST literal()
    {
        if(isNull().isTrue())
            return STImpl.nullST(nullFlavor());
        else
            return STImpl.valueOf(mValue.toString());
    }

    public INT toINT()
    {
        if(isNull().isTrue())
            return INTImpl.nullINT(nullFlavor());
        else
            return INTImpl.valueOf(mValue.intValue());
    }

    public BL lessOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "lessOrEqual");
    }

    public BL compares(QTY qty)
    {
        BLImpl blimpl = null;
        if(qty == null || isNull().or(qty.isNull()).isTrue())
            blimpl = BLImpl.FALSE;
        else
        if(qty.dataType().equal(com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL).isTrue())
            blimpl = BLImpl.TRUE;
        else
            blimpl = BLImpl.FALSE;
        return blimpl;
    }

    public TYPE diffType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL;
    }

    public QTY minus(QTY qty)
    {
        REAL real;
        if(isNull().or(compares(qty).not()).isTrue())
        {
            real = nullREAL(NullFlavorImpl.NA);
        } else
        {
            REAL real1 = (REAL)qty;
            real = valueOf(mValue.subtract(real1.bigDecimalValue()));
        }
        return real;
    }

    public QTY plus(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "plus");
    }

    public BL isZero()
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "isZero");
    }

    public BL lessThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "lessThan");
    }

    public BL greaterOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "greaterOrEqual");
    }

    public BL greaterThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "greaterThan");
    }

    public REAL negated()
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "negated");
    }

    public REAL times(REAL real)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "times");
    }

    public REAL inverted()
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "inverted");
    }

    public INT toInt()
    {
        if(nonNull().booleanValue())
            return INTImpl.valueOf(mValue.longValue());
        else
            return INTImpl.nullINT(nullFlavor());
    }

    public REAL power(REAL real)
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "power");
    }

    public INT precision()
    {
        int i = 0;
        if(isNull().isTrue())
            return INTImpl.nullINT(nullFlavor());
        String s = null;
        if(doubleValue() < 0.0D)
            s = mValue.toString().substring(1);
        else
            s = mValue.toString();
        StringTokenizer stringtokenizer = new StringTokenizer(s.toUpperCase(), "E");
        String s1 = stringtokenizer.nextToken();
        stringtokenizer = new StringTokenizer(s1, ".");
        String s2 = stringtokenizer.nextToken();
        i = countSigDigits(s2);
        if(stringtokenizer.hasMoreTokens())
        {
            String s3 = stringtokenizer.nextToken();
            if(i > 0)
            {
                i += s3.length();
            } else
            {
                i += countSigDigits(s3);
                if(i == 0)
                    i = s3.length() + 1;
            }
        }
        if(i == 0)
            i++;
        return INTImpl.valueOf(i);
    }

    private int countSigDigits(String s)
    {
        int i = 0;
        StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
        for(char c = stringcharacteriterator.first(); c != '\uFFFF' && Character.isDigit(c); c = stringcharacteriterator.next())
        {
            if(c == '0' && i > 0)
                i++;
            if(c != '0')
                i++;
        }

        return i;
    }

    public BL isOne()
    {
        throw ANYImpl.newUnsupportedException("REAL<BigDecimal>", "isOne");
    }

    public double doubleValue()
    {
        return mValue.doubleValue();
    }

    public String stringValue()
    {
        return toString();
    }

    public BigDecimal bigDecimalValue()
    {
        return mValue;
    }

    public String toString()
    {
        if(isNull().isTrue())
            return "<Null:" + nullFlavor().toString() + ">";
        else
            return mValue.toString();
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
            i = 37 * i + mValue.hashCode();
        return i;
    }

}
